package com.example.qxfw.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.Collection;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class SysResult {
    private Integer status; //200业务成功 201业务失败
    private String message;     //服务器提示信息
    private Object result;    //封装后台返回值
    private Object total;   //总条数

    public Integer getStatus() {
        return status;
    }

    public void setStatus(final Integer status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(final String message) {
        this.message = message;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(final Object result) {
        this.result = result;
    }

    public Object getTotal() {
        return total;
    }

    public void setTotal(final Object total) {
        this.total = total;
    }

    public static int[] getRe() {
        return re;
    }

    public static void setRe(final int[] re) {
        SysResult.re = re;
    }

    private static int[] re = new int[]{};

    public static SysResult success() {
        return new SysResult(200, "success", null, null);
    }

    public static SysResult fail() {
        return new SysResult(201, "fail", re, null);
    }

    public static SysResult fail(String msg) {
        return new SysResult(201, msg, re, null);
    }

    public static SysResult success(Object result) {
        return new SysResult(200, "success", result, null);
    }

    public static SysResult success(Object result, Object total) {
        return new SysResult(200, "success", result, total);
    }

    public static SysResult success(String msg, Object result, Object total) {
        return new SysResult(200, msg, result, total);
    }

    public static <T> SysResult common(T res) {
        if (res instanceof Number) {
            Number number = (Number) res;
            if (number.doubleValue() > 0) {
                return SysResult.success(res);
            } else {
                return SysResult.fail("失败");
            }
        } else if (res instanceof Collection<?>) {
            Collection<?> collection = (Collection<?>) res;
            if (collection.isEmpty()) {
                return SysResult.fail("集合为空");
            } else {
                return SysResult.success(res);
            }
        } else {
            return SysResult.fail("无数据");
        }
    }

}
